package 分类.队列和栈;

public class 使括号有效的最少添加 {
    public int minAddToMakeValid(String s) {
         int need=0;
         int res=0;
         for (char c:s.toCharArray()){
             if(c=='('){
                 need++;
             }else {
                 need--;
                 if(need==-1){
                     res++;
                     need=0;
                 }
             }
         }
         return res+need;
    }

}
